﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Beginning.Api.Application.Command.PigBreedings;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.PigWeighings
{
    /// <summary>
    /// 称重类型显示Handler
    /// </summary>
    public class SearchPigWeighingTypeHandler : IRequestHandler<SearchPigWeighingTypeCommand, Result>
    {
        private readonly IMapper mapper;
        /// <summary>
        /// 称重类型
        /// </summary>
        private readonly IBaseRepository<WeighingType> weighingTyperepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchPigWeighingTypeHandler> _logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="weighingTyperepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public SearchPigWeighingTypeHandler(IBaseRepository<WeighingType> weighingTyperepository, IMapper mapper, ILogger<SearchPigWeighingTypeHandler> logger)
        {
            this.weighingTyperepository = weighingTyperepository;
            this.mapper = mapper;
            _logger = logger;
        }
        /// <summary>
        /// 称重类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SearchPigWeighingTypeCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var breedingmethod = await(await weighingTyperepository.GetAll()).ToListAsync();
                result.Data = breedingmethod;
                result.Msg = "称重类型显示成功";
            }
            catch (Exception ex)
            {
                _logger.LogError("称重类型显示失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
